package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.*;
import com.sg.entity.Staffing;
import com.sg.service.base.framework.MStaffingService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.StaffingMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MStaffingServiceImpl
        extends ServiceImpl<StaffingMapper, Staffing>
        implements MStaffingService {

    @Autowired
    StaffingMapper staffingMapper;

    /**
     * 1-3-15查询编制详情[191]
     * gen by moon at 10/16/2022, 11:08:04 AM
     */
    @Trace(operationName = "1-3-15查询编制详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Staffing queryStaffingDetail(QueryStaffingDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<Staffing> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<Staffing> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                Staffing retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-15修改编制[186]
     * gen by moon at 10/16/2022, 11:08:02 AM
     */
    @Trace(operationName = "1-3-15修改编制")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStaffing(Staffing staffing) {
        staffing.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        staffing.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (staffing != null && staffing.getSubjectLifeCycle() != null && staffing.getSubjectLifeCycle().equals("ARCHIVING")) {
            staffing.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        staffing.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        staffing.setReleaseTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (staffing != null && staffing.getSubjectLifeCycle() != null && staffing.getSubjectLifeCycle().equals("ARCHIVING")) {
            staffing.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(staffing);
    }

    /**
     * 1-3-15新增编制[184]
     * gen by moon at 10/16/2022, 11:08:02 AM
     */
    @Trace(operationName = "1-3-15新增编制")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addStaffing(Staffing staffing) {
        staffing.setStaffingId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        staffing.setStaffingNumber("SN");//CUSTOM_CONVENTION
        staffing.setIsArchive("FALSE");
        staffing.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        staffing.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (staffing != null && staffing.getSubjectLifeCycle() != null && staffing.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            staffing.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        staffing.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        staffing.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (staffing != null && staffing.getSubjectLifeCycle() != null && staffing.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            staffing.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        staffing.setTempLock("FALSE");
        staffing.setIsValid("FALSE");
        staffing.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        staffing.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(staffing)) {
            return staffing.getStaffingId();
        } else {
            return "-1";
        }

    }

    /**
     * M计算减值(特殊方法）[1750]
     * gen by moon at 10/16/2022, 11:08:08 AM
     */
    @Trace(operationName = "M计算减值(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCalculationAddAndSub(Staffing staffing) {
        return false;
    }

    /**
     * 1-3-15批量新增编制[1748]
     * gen by moon at 10/16/2022, 11:08:08 AM
     */
    @Trace(operationName = "1-3-15批量新增编制")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddStaffing(List<Staffing> staffing) {

        if (staffing == null || staffing.size() == 0) {
            return false;
        }

        return super.saveBatch(staffing.stream().map(item -> {
            item.setStaffingId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsArchive("FALSE");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
            }
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
            item.setTempLock("FALSE");
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 1-3-14批量查询编制列表[1282]
     */
    @Trace(operationName = "1-3-14批量查询编制列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Staffing> batchQueryStaffingList(BatchQueryStaffingListReq reqDto) {

        try {
            QueryWrapper<Staffing> queryWrapper = new QueryWrapper<>();

            if (reqDto.getStaffingList() == null || reqDto.getStaffingList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("position_id", reqDto.getStaffingList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Staffing>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-14批量删除编制[940]
     * gen by moon at 10/16/2022, 11:08:06 AM
     */
    @Trace(operationName = "1-3-14批量删除编制")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteStaffing(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-3-14批量修改编制[931]
     * gen by moon at 10/16/2022, 11:08:05 AM
     */
    @Trace(operationName = "1-3-14批量修改编制")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateStaffing(List<Staffing> staffing) {

        if (staffing == null || staffing.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(staffing);

    }

    /**
     * 1-3-15查询编制列表[192]
     * gen by moon at 10/16/2022, 11:08:05 AM
     */
    @Trace(operationName = "1-3-15查询编制列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Staffing> queryStaffingList(QueryStaffingListReq reqDto) {

        try {
            QueryWrapper<Staffing> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Staffing.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<Staffing>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-15删除编制[188]
     * gen by moon at 10/16/2022, 11:08:03 AM
     */
    @Trace(operationName = "1-3-15删除编制")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteStaffing(DeleteStaffingReq reqDto) {

        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-14批量查询职务匹配编制列表[1282]
     * gen by moon at 10/16/2022, 11:08:07 AM
     */
    @Trace(operationName = "1-3-14批量查询职务匹配编制列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Staffing> batchQueryPostMatchingStaffing(BatchQueryPostMatchingStaffingReq reqDto) {

        try {
            QueryWrapper<Staffing> queryWrapper = new QueryWrapper<>();

            if (reqDto.getStaffingList() == null || reqDto.getStaffingList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("position_id", reqDto.getStaffingList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Staffing.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Staffing>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-14批量查询编制[3767]
     * gen by moon at 10/16/2022, 11:08:09 AM
     */
    @Trace(operationName = "1-3-14批量查询编制")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Staffing> batchQueryStaffing(BatchQueryStaffingReq reqDto) {

        try {
            QueryWrapper<Staffing> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            if (reqDto.getStaffingList() == null || reqDto.getStaffingList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("staffing_id", reqDto.getStaffingList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Staffing.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Staffing>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1查询编制测试列表[4913]
     * gen by moon at 10/16/2022, 11:08:10 AM
     */
    @Trace(operationName = "M1查询编制测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Staffing> queryStaffingTestDataList(Staffing staffing) {

        try {
            QueryWrapper<Staffing> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(staffing);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Staffing.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Staffing.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Staffing>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删编制测试[4916]
     * gen by moon at 10/16/2022, 11:08:11 AM
     */
    @Trace(operationName = "M1批量删编制测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteStaffingTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
